#include "MapManager.h"

#include "GameTimer.h"
#include "Map.h"
#include "../share/user_define.h"

//-------------------------------------------------------------------------------
MapManager::MapManager(void)
	: GameObject()
	, m_skey2mapptr()
	, m_mkey2mapptr()
{
	_safe_guard_begin;

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
MapManager::~MapManager()
{
	m_skey2mapptr.clear();
	m_mkey2mapptr.clear();
}

//-------------------------------------------------------------------------------
void MapManager::AddMapKey(uint32_t mkey, Map * mapptr)
{
	_safe_guard_begin;

	std::lock_guard<std::recursive_mutex> locker(m_mkey_mutex);

	if (m_mkey2mapptr.empty())
	{
		m_mkey2mapptr.insert(std::make_pair(mkey, mapptr));
		return;
	}

	auto _it = m_mkey2mapptr.find(mkey);
	if (_it == m_mkey2mapptr.end())
	{
		m_mkey2mapptr.insert(std::make_pair(mkey, mapptr));
	}
	else
	{
		m_mkey2mapptr[mkey] = mapptr;
	}

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
Map * MapManager::GetMapPtrByMapKey(uint32_t mkey)
{
	_safe_guard_begin;

	std::lock_guard<std::recursive_mutex> locker(m_mkey_mutex);

	auto _it = m_mkey2mapptr.find(mkey);
	if (_it == m_mkey2mapptr.end())
	{
		return nullptr;
	}

	return _it->second;

	_safe_guard_end;

	return nullptr;
}

//-------------------------------------------------------------------------------
void MapManager::AddSceneToSecenMap(uint32_t skey, Map * mapptr)
{
	_safe_guard_begin;

	std::lock_guard<std::recursive_mutex> locker(m_skey_mutex);

	auto _it = m_skey2mapptr.find(skey);
	if (_it == m_skey2mapptr.end())
	{
		m_skey2mapptr.insert(std::make_pair(skey, mapptr));
	}
	else
	{
		m_skey2mapptr[skey] = mapptr;
	}

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
Map * MapManager::GetScenePtrBySceneKey(uint32_t skey)
{
	_safe_guard_begin;

	std::lock_guard<std::recursive_mutex> locker(m_skey_mutex);

	auto _it = m_skey2mapptr.find(skey);
	if (_it == m_skey2mapptr.end())
	{
		return nullptr;
	}

	return _it->second;

	_safe_guard_end;

	return nullptr;
}

void MapManager::EraseKeyFromSecenMap(uint32_t skey)
{
	_safe_guard_begin;

	std::lock_guard<std::recursive_mutex> locker(m_skey_mutex);

	auto _it = m_skey2mapptr.find(skey);
	if (_it == m_skey2mapptr.end())
	{
		return;
	}

	m_skey2mapptr.erase(_it);

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
